﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using SIPO.Library;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.Enum;
using SIPO.Library.Interfaces;

namespace SIPO.Library.DataAccess.Administration.Catalog
{
    public partial class recUserSectorRepo : ISearchable<recUserSector>
    {
        public List<recUserSector> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<recUserSector> tt = new List<recUserSector>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat("      UserSectorID, RegisterSectorID, DebtorSectorID, SubSubSectorID, UserTypeID, UserID, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy ");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat("      master.recUserSector ");
                    sbQuery.AppendFormat("      {0} ", parameters);
                    sbQuery.AppendFormat(" ) ");
                    sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recUserSector t = new recUserSector();
                                t.UserSectorID = dr[recUserSector.Fields.UserSectorID] == DBNull.Value ? 0 : int.Parse(dr[recUserSector.Fields.UserSectorID].ToString());
                                t.RegisterSectorID = dr[recUserSector.Fields.RegisterSectorID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserSector.Fields.RegisterSectorID].ToString());
                                t.DebtorSectorID = dr[recUserSector.Fields.DebtorSectorID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserSector.Fields.DebtorSectorID].ToString());
                                t.SubSubSectorID = dr[recUserSector.Fields.SubSubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recUserSector.Fields.SubSubSectorID].ToString());
                                t.UserTypeID = dr[recUserSector.Fields.UserTypeID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserSector.Fields.UserTypeID].ToString());
                                t.UserID = dr[recUserSector.Fields.UserID] == DBNull.Value ? 0 : int.Parse(dr[recUserSector.Fields.UserID].ToString());
                                t.CreatedDate = dr[recUserSector.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recUserSector.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recUserSector.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recUserSector.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recUserSector.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recUserSector.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recUserSector.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recUserSector.Fields.UpdatedBy].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                        case SQL.Function.Aggregate.Max:
                            sbQuery.AppendFormat("SELECT MAX({0}) FROM master.recUserSector {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Min:
                            sbQuery.AppendFormat("SELECT MIN({0}) FROM master.recUserSector {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Distinct:
                            sbQuery.AppendFormat("SELECT DISTINCT({0}) FROM master.recUserSector {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM master.recUserSector {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Sum:
                            sbQuery.AppendFormat("SELECT SUM({0}) FROM master.recUserSector {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Avg:
                            sbQuery.AppendFormat("SELECT AVG({0}) FROM master.recUserSector {1}", column, parameters);
                            break;
                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public List<recUserSector> GetListByUserId(int key)
        {
            List<recUserSector> tt = new List<recUserSector>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT UserSectorID, RegisterSectorID, DebtorSectorID, SubSubSectorID, UserTypeID, UserID, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy FROM master.recUserSector  WHERE UserID = @UserID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = key;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recUserSector t = new recUserSector();
                                t.UserSectorID = dr[recUserSector.Fields.UserSectorID] == DBNull.Value ? 0 : int.Parse(dr[recUserSector.Fields.UserSectorID].ToString());
                                t.RegisterSectorID = dr[recUserSector.Fields.RegisterSectorID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserSector.Fields.RegisterSectorID].ToString());
                                t.DebtorSectorID = dr[recUserSector.Fields.DebtorSectorID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserSector.Fields.DebtorSectorID].ToString());
                                t.SubSubSectorID = dr[recUserSector.Fields.SubSubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recUserSector.Fields.SubSubSectorID].ToString());
                                t.UserTypeID = dr[recUserSector.Fields.UserTypeID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserSector.Fields.UserTypeID].ToString());
                                t.UserID = dr[recUserSector.Fields.UserID] == DBNull.Value ? 0 : int.Parse(dr[recUserSector.Fields.UserID].ToString());
                                t.CreatedDate = dr[recUserSector.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recUserSector.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recUserSector.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recUserSector.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recUserSector.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recUserSector.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recUserSector.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recUserSector.Fields.UpdatedBy].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public recUserSector GetByRegisterSectorID(int key)
        {
            recUserSector t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT UserSectorID, RegisterSectorID, DebtorSectorID, SubSubSectorID, UserTypeID, UserID, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy FROM master.recUserSector WHERE RegisterSectorID = @RegisterSectorID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@RegisterSectorID", SqlDbType.Int).Value = key;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new recUserSector();
                                t.UserSectorID = dr[recUserSector.Fields.UserSectorID] == DBNull.Value ? 0 : int.Parse(dr[recUserSector.Fields.UserSectorID].ToString());
                                t.RegisterSectorID = dr[recUserSector.Fields.RegisterSectorID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserSector.Fields.RegisterSectorID].ToString());
                                t.DebtorSectorID = dr[recUserSector.Fields.DebtorSectorID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserSector.Fields.DebtorSectorID].ToString());
                                t.SubSubSectorID = dr[recUserSector.Fields.SubSubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recUserSector.Fields.SubSubSectorID].ToString());
                                t.UserTypeID = dr[recUserSector.Fields.UserTypeID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserSector.Fields.UserTypeID].ToString());
                                t.UserID = dr[recUserSector.Fields.UserID] == DBNull.Value ? 0 : int.Parse(dr[recUserSector.Fields.UserID].ToString());
                                t.CreatedDate = dr[recUserSector.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recUserSector.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recUserSector.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recUserSector.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recUserSector.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recUserSector.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recUserSector.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recUserSector.Fields.UpdatedBy].ToString();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }
        public void DeleteByDebtorSectorID(int DebtorSectorID)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "delete from master.recusersector where debtorsectorid=@Debtorsectorid";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@Debtorsectorid", SqlDbType.Int).Value = DebtorSectorID;
                      try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                      
                      }
                      catch (Exception ex)
                      {
                          throw (ex);
                      }
                      finally
                      {
                          if (conn.State == ConnectionState.Open) conn.Close();
                      }
                    }
            }
        }
    }

}
